import { StorageManager } from "@/managers/StorageManager.js";
import { AudioManager } from "@/managers/AudioManager.js";

export const config={
    width:816,
    height:624,
    iconSize:32,
    lineHeight:36,
    animationSpeed:12
}

export class ConfigManager {
    static alwaysDash = false;
    static commandRemember = false;

    constructor() {
        throw new Error('This is a static class');
    }

    static get bgmVolume() {
        return AudioManager.bgmVolume;
    }

    static set bgmVolume(value) {
        AudioManager.bgmVolume = value;
    }

    static get bgsVolume() {
        return AudioManager.bgsVolume;
    }

    static set bgsVolume(value) {
        AudioManager.bgsVolume = value;
    }

    static get meVolume() {
        return AudioManager.meVolume;
    }

    static set meVolume(value) {
        AudioManager.meVolume = value;
    }

    static get seVolume() {
        return AudioManager.seVolume;
    }

    static set seVolume(value) {
        AudioManager.seVolume = value;
    }

    static load() {
        let config = {};
        let json = StorageManager.load(-1);
        if (json) {
            config = JSON.parse(json);
        }
        this.applyData(config);
    }

    static save() {
        StorageManager.save(-1, JSON.stringify(this.makeData()));
    }

    static makeData() {
        return {
            alwaysDash: this.alwaysDash,
            commandRemember: this.commandRemember,
            bgmVolume: this.bgmVolume,
            bgsVolume: this.bgsVolume,
            meVolume: this.meVolume,
            seVolume: this.seVolume,
        };
    }

    static applyData(config) {
        let {
            alwaysDash,
            commandRemember,
            bgmVolume=100,
            bgsVolume=100,
            meVolume=100,
            seVolume=100
        }=config;
        this.alwaysDash = !!alwaysDash;
        this.commandRemember = !!commandRemember;
        this.bgmVolume = Number(bgmVolume);
        this.bgsVolume = Number(bgsVolume);
        this.meVolume = Number(meVolume);
        this.seVolume = Number(seVolume);
    }
}